<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@include file="../includes/tag.jsp" %>
<%@taglib prefix="bstag" uri="/core_taglib" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>Wcms内容管理系统</title>
    <script type="text/javascript" src="<%=basePath%>resources/lib/jquery/jquery.js"></script>
    <script type="text/javascript" src="<%=basePath%>resources/lib/webui/buss.ui.core.js"></script>
    <script type="text/javascript" src="<%=basePath%>resources/lib/webui/buss.ui.js"></script>
	<script type="text/javascript" src="<%=basePath%>resources/lib/jqgrid/js/i18n/grid.locale-cn.js"></script>
	<script type="text/javascript" src="<%=basePath%>resources/lib/jqgrid/js/jquery.jqGrid.min.js"></script>
	<script type="text/javascript" src="<%=basePath%>resources/lib/public.js"></script>
	
	<link rel="stylesheet" type="text/css" href="<%=basePath%>resources/lib/styles/themes/buss-ui.css">
	<link rel="stylesheet" type="text/css" href="<%=basePath%>resources/lib/jqgrid/css/jquery-ui.css">
	<link rel="stylesheet" type="text/css" href="<%=basePath%>resources/lib/jqgrid/css/ui.jqgrid.css">
    <link rel="stylesheet" type="text/css" href="<%=basePath%>resources/styles/public.css">
    <link rel="stylesheet" type="text/css" href="<%=basePath%>resources/styles/icon.css">

	<script type="text/javascript">
		
		$(document).ready(function(){						
			setGridData();
			loadTree();
			resize();
			$(window).resize(resize);
			$("#search-btn").button({iconCls:'icon-search',handler:function(){
    			var jsonParam = syscore.formToJson($("#_searchForm"));
				$("#tabledata").jqGrid("setGridParam",{page:1,postData:jsonParam}).trigger("reloadGrid");
			}});
			$("#search-clear").button({iconCls:'icon-cancel',handler:function(){
				$("#_searchForm").resetForm();
    			$("#search-btn").trigger("click");
			}});
			$("#add-btn").button({iconCls:'icon-add',handler:function(){
				var node = $("#catagoryTree").treeview("getSelectedNode");
				if(!node){
					$.alert('提示','请选择要增加分类的父节点!',300,140,'info');
					return;
				}
				editPage("addpage",node.id);				
			}});		
			$("#edit-btn").button({iconCls:'icon-edit',handler:function(){
				var selRows = $("#tabledata").jqGrid('getGridParam',"selarrrow");
				if(selRows == 0){
					$.alert('提示','你没有选择要修改的数据!',300,140,'info');
					return;
				}
				if(selRows.length > 1){
					$.alert('提示','只能选择一条数据，请重新选择!',300,140,'info');
					return;
				}
				editPage("editpage",selRows[0]);				
			}});
			$("#del-btn").button({iconCls:'icon-remove',handler:function(){
				var selRows = $("#tabledata").jqGrid('getGridParam',"selarrrow");
				if(selRows == 0){
					$.alert('提示','你没有选择要删除的数据!',300,140,'info');
					return;
				}
				$.confirm('提示','确认要删除选中数据吗?',300,140,function(){
					var str = "";
					for(var i = 0;i < selRows.length;i++){
						if(i < selRows.length - 1){
							str += "ids=" + selRows[i] + "&";
						}else{
							str += "ids=" + selRows[i];
						}
					}
					$.post("<%=basePath%>admin/goodscategory/delete.do",str,function(rsData){
						if(rsData.message.code != '0'){
							$.alert('错误','删除商品分类信息失败!原因:' + rsData.message.mess,300,140,'error');
							return;
						}
						for(var i = 0;i < selRows.length;i++){
							 var node = $("#catagoryTree").treeview("getNodeByParam","id",selRows[i]);        				
	        				 $("#catagoryTree").treeview("removeNode",node);			        				 
						}
						$("#tabledata").jqGrid({page:1}).trigger('reloadGrid');
					},"json");
				});				
			}});
			
			$("#propgroup-btn").button({iconCls:'icon-add',handler:function(){
				var selRows = $("#tabledata").jqGrid('getGridParam',"selarrrow");
				if(selRows == 0){
					$.alert('提示','你没有选择要设置属性分组的数据!',300,140,'info');
					return;
				}
				if(selRows.length > 1){
					$.alert('提示','只能选择一条数据，请重新选择!',300,140,'info');
					return;
				}
				var editGroupDlg = $("<div/>").dialog({
					height:350,
					width:472,
					title:'设置分类关联属性分组',
					iconCls:'icon',
					loadUrl:'<%=basePath%>admin/goodscategory/propgrouppage.do?categoryId=' + selRows[0],
					buttons:[
					         {text:'保 存',type:'button',iconCls:'icon-save',handler:function(){
					        	 editGroupDlg.find("#sel_group_list option").each(function(){
					        		if($(this).attr("categoryId") != selRows[0]){
					        			$(this).attr("selected","");
					        		} else{
					        			$(this).attr("selected","true");
					        		}
					        	 });
					        	 syscore.ajaxSubmit(editGroupDlg.find("form"),function(rsData){
					        		 if(rsData.message.code != '0'){
										 $.alert('错误','设置商品分类关联商品属性分组失败!原因:' + rsData.message.mess,300,140,'error'); 
										 return;
									 }
					        		 editGroupDlg.dialog("destroy");
					        	 });
					         }},
					         {text:'关 闭',type:'button',iconCls:'icon-cancel',handler:function(){
					        	 editGroupDlg.dialog("destroy");
					         }}
					        ],
					 onLoad:function(){
						 editGroupDlg.find("#addGroup").button({handler:function(){
							 if(editGroupDlg.find("#group_list > option:selected").length == 0){
								 $.alert('提示','你没有选择要添加的属性分组!',300,140,'info');
								 return;
							 }
							 editGroupDlg.find("#group_list > option:selected").each(function(){
								 editGroupDlg.find("#sel_group_list").append($(this));
							 })
						 }});
						 editGroupDlg.find("#group_list").dblclick(function(){
							 editGroupDlg.find("#addGroup").trigger("click");
						 });
						 editGroupDlg.find("#sel_group_list").dblclick(function(){
							 editGroupDlg.find("#removeGroup").trigger("click");
						 });
						 editGroupDlg.find("#removeGroup").button({handler:function(){
							 if(editGroupDlg.find("#sel_group_list > option:selected").attr("categoryId") != selRows[0]){
								 $.alert('提示','不能删除上级分类对应的属性分组信息!',300,140,'info');
								 return;
							 }
							 if(editGroupDlg.find("#sel_group_list > option:selected").length == 0){
								 $.alert('提示','你没有选择要删除的属性分组!',300,140,'info');
								 return;
							 }
							 editGroupDlg.find("#sel_group_list > option:selected").each(function(){
								 editGroupDlg.find("#group_list").append($(this));
							 })
						 }});
						 editGroupDlg.find("#query_unselgroup").keyup(function(){						
							 editGroupDlg.find("#group_list option").hide();
							 editGroupDlg.find("#group_list option:Contains('"+$(this).val()+"')").show();
						 });
						 editGroupDlg.find("#query_selgroup").keyup(function(){						
							 editGroupDlg.find("#sel_group_list option").hide();
							 editGroupDlg.find("#sel_group_list option:Contains('"+$(this).val()+"')").show();
						 });
					 }
				});
				editGroupDlg.dialog("loadPage");
				
			}});
			$("#brand-btn").button({iconCls:'icon-add',handler:function(){
				var selRows = $("#tabledata").jqGrid('getGridParam',"selarrrow");
				if(selRows == 0){
					$.alert('提示','你没有选择要关联的分类信息!',300,140,'info');
					return;
				}
				if(selRows.length > 1){
					$.alert('提示','只能选择一条数据，请重新选择!',300,140,'info');
					return;
				}
				var editBrandDlg = $("<div/>").dialog({
					height:350,
					width:472,
					title:'设置分类关联品牌',
					iconCls:'icon',
					loadUrl:'<%=basePath%>admin/goodscategory/brandpage.do?categoryId=' + selRows[0],
					buttons:[
					         {text:'保 存',type:'button',iconCls:'icon-save',handler:function(){
					        	 editBrandDlg.find("#sel_brand_list option").attr("selected","true");
					        	 syscore.ajaxSubmit(editBrandDlg.find("form"),function(rsData){
					        		 if(rsData.message.code != '0'){
										 $.alert('错误','设置商品分类关联品牌失败!原因:' + rsData.message.mess,300,140,'error'); 
										 return;
									 }
									 editBrandDlg.dialog("destroy");
					        	 });
					         }},
					         {text:'关 闭',type:'button',iconCls:'icon-cancel',handler:function(){
					        	 editBrandDlg.dialog("destroy");
					         }}
					        ],
					 onLoad:function(){
						 editBrandDlg.find("#addBrand").button({handler:function(){
							 if(editBrandDlg.find("#brand_list > option:selected").length == 0){
								 $.alert('提示','你没有选择要添加的品牌!',300,140,'info');
								 return;
							 }
							 editBrandDlg.find("#brand_list > option:selected").each(function(){
								 editBrandDlg.find("#sel_brand_list").append($(this));
							 })
						 }});
						 editBrandDlg.find("#brand_list").dblclick(function(){
							 editBrandDlg.find("#addBrand").trigger("click");
						 });
						 editBrandDlg.find("#sel_brand_list").dblclick(function(){
							 editBrandDlg.find("#removeBrand").trigger("click");
						 });
						 editBrandDlg.find("#removeBrand").button({handler:function(){
							 if(editBrandDlg.find("#sel_brand_list > option:selected").length == 0){
								 $.alert('提示','你没有选择要删除的品牌!',300,140,'info');
								 return;
							 }
							 editBrandDlg.find("#sel_brand_list > option:selected").each(function(){
								 editBrandDlg.find("#brand_list").append($(this));
							 })
						 }});
						 editBrandDlg.find("#query_unselbrand").keyup(function(){						
							 editBrandDlg.find("#brand_list option").hide();
							 editBrandDlg.find("#brand_list option:Contains('"+$(this).val()+"')").show();
						 });
						 editBrandDlg.find("#query_selbrand").keyup(function(){						
							 editBrandDlg.find("#sel_brand_list option").hide();
							 editBrandDlg.find("#sel_brand_list option:Contains('"+$(this).val()+"')").show();
						 });
					 }
				});
				editBrandDlg.dialog("loadPage");
			}});
		});
		
		/**编辑属性页面*/
    	function editPage(type,id){    		
    		var url = "<%=basePath%>admin/goodscategory/";
    		var pageUrl = "";
    		var saveUrl = "";
    		var title = "新增商品分类信息";
    		if(type == "editpage"){
    			title = "修改商品分类信息";
    			saveUrl = url + "edit.do";
    			pageUrl = url + "editpage.do?id=" + id;
    		}else{
    			saveUrl = url + "add.do";
    			pageUrl = url + "addpage.do?id=" + id;
    		}
    		var editDlg = syscore.editDialog(title,pageUrl,"",500,318,saveUrl,function(rsData){
    			if(rsData.message.code != '0'){
					 $.alert('提示','保存数据失败!原因:' + rsData.message.mess,300,140,'error');
					 return false;
				 }else{	
					 var tId = "";
					 if(type == "editpage"){
						 tId = id;
						 var node = $("#catagoryTree").treeview("getNodeByParam","id",tId);						 
						 node.name = editDlg.find("input[name='name']").val();
						 node.code = editDlg.find("input[name='code']").val();
						 $("#catagoryTree").treeview("updateNode",node);
					 }else{
						 tId = rsData.category.id;
						 var node = $("#catagoryTree").treeview("getSelectedNode");
	       				 $("#catagoryTree").treeview("addNodes",node,rsData.category);
					 }				 
					 
   			 		 $("#tabledata").jqGrid().trigger('reloadGrid');			        			 	
   			 		 editDlg.dialog("destroy");
				 }
    		},function(){
    			editDlg.find("#isPub").change(function(){
    				if ($(this).attr("checked")){
    					editDlg.find("input[name='branchId']").attr("value","0");
    				}else{
    					editDlg.find("input[name='branchId']").attr("value","");
    				}
    			});    			
    		});
    	}
		
		function postEditMenu(menuId){
			editSysmenu(menuId);
		}
		
		function setGridData(){
			var url = "<%=basePath%>admin/goodscategory/list.do?parentId=0";
			var colNames = ['分类编码','分类名称', '当前状态','描述'];
			var colModel = [
			                {name:'code',index:'code', width:60,align:'left'},
						    {name:'name',index:'name', width:150,align:'left'},							 
							{name:'status',index:'status', width:60,align:'center',formatter:syscore.formaterStatus}, 
							{name:'intro', align:'left',sortable:false},
						];
			syscore.setGridData("#tabledata","tablepage",url,colNames,colModel,true);
		}	
		
		function loadTree(){
			var root = [{
				id:"0",
				code:'catetory_root',
				name:'商品分类信息',			
				isParent:true,
				subCategories:true,
				open:true
			}];
			$("#catagoryTree").treeview({
				treeNodeKey:'id',
				treeNodeParentKey:'parentId',
				hasChildKey:'subCategories',
				nameCol:'name',
				expandSpeed:"",
				//asyncParam:['id'],
				asyncParamOther:["isPage",false],
				async:true,
				asyncUrl:function(treeNode){
					return "<%=basePath%>admin/goodscategory/list.do?parentId=" + treeNode.id;	
				},
				root:{isRoot:true,nodes:root},
				callback:{
					beforeAsyncSuccess:function(msg){
						if(msg.message.code == '-2'){
							$.alert('提示','你还没有登录或Session已经过期，请重新登录系统!',300,170,'info');
							return false;
						}
						if(msg.message.code == "-1"){
							$.alert("提示","获取信息失败，原因：" + msg.message.mess,300,170,'error');
							return false;
						}
						return true;
					},
					click:function(event,treeId,treeNode){
						var url = $("#tabledata").jqGrid("getGridParam","url");
						$("#tabledata").jqGrid("setGridParam",{"url":"<%=basePath%>admin/goodscategory/list.do?parentId="+treeNode.id}).trigger('reloadGrid');
					}
				}
			});
		}

		function resize(){
			var h = $(window).height() - $(".nav_curloc").outerHeight() - $(".nav_search").outerHeight() - $(".nav_btn_bar").outerHeight() - 10;
			$(".context").height(h);
			$("#tabledata").jqGrid("setGridHeight",h - 50);
			$("#tabledata").jqGrid("setGridWidth",$(window).width() - $("#catagoryTree").parent().width() - 12);
		}
		
	</script>
  </head>
  
  <body>
  	<div style="border-right:1px solid #68C4E9;position:absolute;width:180px;height:100%;left:0;overflow:auto;">
  		<ul id="catagoryTree" class="tree"></ul>
  	</div>
  	<div style="height:100%;margin:0 0 0 181px;position:relative;">
  		<div class="nav_curloc">
	    	<label>您的当前位置：<bstag:navbar menuId="${menuid}"/></label>
	    </div>
	     <div class="nav_search" style="height:28px;">
	     	<form action="" id='_searchForm' name="_searchForm">
		    	<table height="100%" cellpadding="0" cellspacing="0">
		    		<tr>
		    			<td valign="middle">分类名称：</td>
		    			<td valign="middle">
		    				<input type="text" size="25" maxlength="100" class="text" name="name"/>
		    			</td>
		    			<td style="padding-left:5px;">
		    				<a id="search-btn" href="javascript:void(0);">查 询</a>
		    			</td>
		    			<td style="padding-left:5px;">
			    			<a id="search-clear" href="javascript:void(0);">清 空</a>
			    		</td>
		    		</tr>
		    	</table>
	    	</form>
	    </div>
	    <div class="nav_btn_bar">
	    	<table height="100%" cellpadding="0" cellspacing="0">
	    		<tr>
	    			<td valign="middle">
	    				<a id="add-btn" href="javascript:void(0);">新 增</a>
	    			</td>
	    			<td valign="middle"" style="padding-left:5px;">
	    				<a id="edit-btn" href="javascript:void(0);">修 改</a>
	    			</td>
	    			<td valign="middle" style="padding-left:5px;">
	    				<a id="del-btn" href="javascript:void(0);">删 除</a>
	    			</td>
	    			<td valign="middle" style="padding-left:5px;">
	    				<a id="propgroup-btn" href="javascript:void(0);">设置属性分组</a>
	    			</td>
	    			<td valign="middle" style="padding-left:5px;">
	    				<a id="brand-btn" href="javascript:void(0);">关联品牌</a>
	    			</td>
	    		</tr>
	    	</table>
	    </div>
	    <div class="context">
	    	<table id="tabledata"></table>
	    	<div id="tablepage"></div>
	    </div>
  	</div>     
  </body>
</html>
